package com.example.core.service.impl;

import com.example.core.convert.context.RequestContext;
import com.example.core.form.BaseForm;
import com.example.core.model.BaseModel;
import com.example.core.service.BaseCoreService;
import com.example.core.service.BaseEventService;
import com.example.core.service.DispatchService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class DispatchServiceImpl implements DispatchService {

    @Resource
    private List<BaseCoreService> coreServices = new ArrayList<>();

    @Resource
    private List<BaseEventService> eventServices = new ArrayList<>();

    @Override
    public void add(BaseModel model) throws Exception {
        for (BaseCoreService coreService : this.coreServices) {
            coreService.add(model);
        }
        for (BaseEventService eventService : this.eventServices) {
            eventService.add(model);
        }
    }

    @Override
    public void delete(BaseModel model) throws Exception {
        for (BaseCoreService coreService : this.coreServices) {
            coreService.delete(model);
        }
        for (BaseEventService eventService : this.eventServices) {
            eventService.delete(model);
        }
    }

    @Override
    public void update(BaseModel model) throws Exception {
        for (BaseCoreService coreService : this.coreServices) {
            coreService.update(model);
        }
        for (BaseEventService eventService : this.eventServices) {
            eventService.update(model);
        }
    }

    @Override
    public void cloneModelToForm(BaseForm form, RequestContext requestContext, BaseModel model) throws Exception {
        for (BaseCoreService coreService : this.coreServices) {
            coreService.cloneModelToForm(form, requestContext, model);
        }
    }

    @Override
    public void convertFormToModel(BaseForm form, RequestContext requestContext, BaseModel model) throws Exception {
        for (BaseCoreService coreService : this.coreServices) {
            coreService.convertFormToModel(form, requestContext, model);
        }
    }
}
